package com.yiwei.zhh.adapter;

/**
 * Created by xsooy-pc on 18-6-21.
 */

import java.util.List;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.yiwei.zhh.R;
import com.yiwei.zhh.activity.ChsiActivity;
import com.yiwei.zhh.activity.EmailIdentityActivity;
import com.yiwei.zhh.constant.Const;
import com.yiwei.zhh.model.DiscoverIndexModel;
import com.yiwei.zhh.util.PreferenceUtils;
import com.yiwei.zhh.util.ToastUtil;
import com.yiwei.zhh.util.Util;


/**
 * GridView加载数据的适配器
 *
 * @author Administrator
 */
public class MyGridViewAdpter extends BaseAdapter {

    private Context context;
    private List<DiscoverIndexModel.DataBean.CertListBean> lists;//数据源
    private int mIndex; // 页数下标，标示第几页，从0开始
    private int mPargerSize;// 每页显示的最大的数量


    public MyGridViewAdpter(Context context, List<DiscoverIndexModel.DataBean.CertListBean> lists,
                            int mIndex, int mPargerSize) {
        this.context = context;
        this.lists = lists;
        this.mIndex = mIndex;
        this.mPargerSize = mPargerSize;
    }

    /**
     * 先判断数据及的大小是否显示满本页lists.size() > (mIndex + 1)*mPagerSize
     * 如果满足，则此页就显示最大数量lists的个数
     * 如果不够显示每页的最大数量，那么剩下几个就显示几个
     */
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return lists.size() > (mIndex + 1) * mPargerSize ?
                mPargerSize : (lists.size() - mIndex * mPargerSize);
    }

    @Override
    public DiscoverIndexModel.DataBean.CertListBean getItem(int arg0) {
        // TODO Auto-generated method stub
        return lists.get(arg0 + mIndex * mPargerSize);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0 + mIndex * mPargerSize;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_v3_identity, null);

            holder.txtStatus = (SuperButton) convertView.findViewById(R.id.txt_status);
            holder.txtPoint = (TextView) convertView.findViewById(R.id.txt_point);
            holder.txtType = (TextView) convertView.findViewById(R.id.txt_type);
            holder.imgIcon = (ImageView) convertView.findViewById(R.id.img_icon);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //重新确定position因为拿到的总是数据源，数据源是分页加载到每页的GridView上的
        int pos = position + mIndex * mPargerSize;//假设mPageSiez
        DiscoverIndexModel.DataBean.CertListBean item = lists.get(pos);
        holder.txtType.setText(item.name);
        holder.txtPoint.setText("+" + item.rewards);
        if (item.iscert.equals("2")) {
            holder.txtStatus.setShapeSolidColor(0xffFF7A31).setUseShape();
            holder.txtStatus.setText("认证中");
            holder.txtStatus.setVisibility(View.VISIBLE);
        }else if (item.iscert.equals("3")) {
            holder.txtStatus.setShapeSolidColor(0xffFE5A59).setUseShape();
            holder.txtStatus.setText("需更新");
            holder.txtStatus.setVisibility(View.VISIBLE);
        } else {
            holder.txtStatus.setVisibility(View.INVISIBLE);
        }


        switch (item.remark) {
            case Const.IdentityType.yunyin:
                if (item.iscert.equals("0")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_yunyin_no);
                } else if (item.iscert.equals("2")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_yunyin_ing);
                } else if (item.iscert.equals("1")||item.iscert.equals("3")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_yunyin_yes);
                }
                break;
            case Const.IdentityType.car:
                if (item.iscert.equals("0")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_car_no);
                } else if (item.iscert.equals("2")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_car_ing);
                } else if (item.iscert.equals("1")||item.iscert.equals("3")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_car_yes);
                }
                break;
            case Const.IdentityType.contacts:
                if (item.iscert.equals("0")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_contacts_no);
                } else if (item.iscert.equals("2")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_contacts_ing);
                } else if (item.iscert.equals("1")||item.iscert.equals("3")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_contacts_yes);
                }
                break;

            case Const.IdentityType.email:
                if (item.iscert.equals("0")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_email_no);
                } else if (item.iscert.equals("2")) {
//                    holder.imgIcon.setImageResource(R.mipmap.img_email_ing);
                } else if (item.iscert.equals("1")||item.iscert.equals("3")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_email_yes);
                }
                break;

            case Const.IdentityType.face_detect:
                if (item.iscert.equals("0")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_face_detect_no);
                } else if (item.iscert.equals("2")) {
//                    holder.imgIcon.setImageResource(R.mipmap.img_face_detect_ing);
                } else if (item.iscert.equals("1")||item.iscert.equals("3")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_face_detect_yes);
                }
                break;

            case Const.IdentityType.house:
                if (item.iscert.equals("0")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_house_no);
                } else if (item.iscert.equals("2")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_house_ing);
                } else if (item.iscert.equals("1")||item.iscert.equals("3")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_house_yes);
                }
                break;

            case Const.IdentityType.housefund:
                if (item.iscert.equals("0")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_housefund_no);
                } else if (item.iscert.equals("2")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_housefund_ing);
                } else if (item.iscert.equals("1")||item.iscert.equals("3")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_housefund_yes);
                }
                break;

            case Const.IdentityType.income:
                if (item.iscert.equals("0")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_income_no);
                } else if (item.iscert.equals("2")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_income_ing);
                } else if (item.iscert.equals("1")||item.iscert.equals("3")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_income_yes);
                }
                break;

            case Const.IdentityType.jd:
                if (item.iscert.equals("0")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_jd_no);
                } else if (item.iscert.equals("2")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_jd_ing);
                } else if (item.iscert.equals("1")||item.iscert.equals("3")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_jd_yes);
                }
                break;
            case Const.IdentityType.linkedin:
                if (item.iscert.equals("0")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_linkedin_no);
                } else if (item.iscert.equals("2")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_linkedin_ing);
                } else if (item.iscert.equals("1")||item.iscert.equals("3")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_linkedin_yes);
                }
                break;

            case Const.IdentityType.realname:
                if (item.iscert.equals("0")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_realname_no);
                } else if (item.iscert.equals("2")) {
//                    holder.imgIcon.setImageResource(R.mipmap.img_realname_ing);
                } else if (item.iscert.equals("1")||item.iscert.equals("3")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_realname_yes);
                }
                break;

            case Const.IdentityType.socialsecurity:
                if (item.iscert.equals("0")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_socialsecurity_no);
                } else if (item.iscert.equals("2")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_socialsecurity_ing);
                } else if (item.iscert.equals("1")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_socialsecurity_yes);
                }
                break;

            case Const.IdentityType.taobao:
                if (item.iscert.equals("0")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_taobao_no);
                } else if (item.iscert.equals("2")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_taobao_ing);
                } else if (item.iscert.equals("1")||item.iscert.equals("3")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_taobao_yes);
                }
            case Const.IdentityType.work:
                if (item.iscert.equals("0")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_work_no);
                } else if (item.iscert.equals("2")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_work_ing);
                } else if (item.iscert.equals("1")||item.iscert.equals("3")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_work_yes);
                }

            case Const.IdentityType.xuexin:
                if (item.iscert.equals("0")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_xuexin_no);
                } else if (item.iscert.equals("2")) {
//                    holder.imgIcon.setImageResource(R.mipmap.img_xuexin_ing);
                } else if (item.iscert.equals("1")||item.iscert.equals("3")) {
                    holder.imgIcon.setImageResource(R.mipmap.img_xuexin_yes);
                }
                break;
        }

//        final int pos = position + mIndex * mPargerSize;//假设mPageSiez
        //假设mPagerSize=8，假如点击的是第二页（即mIndex=1）上的第二个位置item(position=1),那么这个item的实际位置就是pos=9
//        holder.tv_name.setText(lists.get(pos).getName() + "");
//        holder.iv_nul.setImageResource(lists.get(pos).getUrl());
        //添加item监听
//        convertView.setOnClickListener(new View.OnClickListener() {
//
//            @Override
//            public void onClick(View arg0) {
//                // TODO Auto-generated method stub
//                Toast.makeText(context, "您点击了"  + lists.get(pos).getName(), Toast.LENGTH_SHORT).show();
//            }
//        });
        return convertView;
    }

    static class ViewHolder {
        private SuperButton txtStatus;
        private TextView txtPoint;
        private TextView txtType;
        private ImageView imgIcon;
    }
}
